<?php

function seo_baiviet($id,$str1)
{
//return BASE_URL.$id."_".$str1.".htm";
return "index.php?module=news&act=view&id=".$id;
}





function seo_category($id,$str1)
{
//return BASE_URL."c".$id."/".trim($str1)."/";
return "index.php?module=news&act=list&cat=".$id;
}



function seo_page($id,$str1)
{
//return BASE_URL."p".$id."/".trim($str1)."/";
return "index.php?module=news&act=list&cat=".$id;
}



function seo_subcategory($id,$str2)
{
//return BASE_URL."s".$id."/".$str2."/";
return "index.php?module=news&act=list&sub=".$id;

}

function category_info($catid){
	$cache=new cache;
	$cat_info=$cache->load("cat".$catid);

	
	if(!$cat_info)
	{
		$mnews=new Model_News;
		$mnews->get_catbyid($catid);
		$row=$mnews->fetchOne();
		$row['url']=seo_category($row['cat_id'],$row['cat_alias']);
		$mnews->query("select * from news_subcategory where category=".$row['cat_id']." order by thutu asc");
	
		if($mnews->num_rows()>0){
					$datasub=$mnews->fetchAll();
					$datasub2=array();
					foreach($datasub as $rs){
					$rs1=$rs;
					$rs1['url']=seo_subcategory($rs['sub_id'],$rs['sub_alias']);
					$datasub2[]=$rs1;
					}
					$row['sub']=$datasub2;
		}
		
		if($row)
		{
			$cache->save("cat".$catid,$row,10000);
			
			$cat_info=$row;
			return $cat_info;
		}
		else
		{
	return false;
		}
		
	}
	return $cat_info;

}

function subcategory_info($sub){
	$cache=new cache;
	$sub_info=$cache->load("sub".$sub);
	if(!$sub_info)
	{
		$mnews=new Model_News;
		$mnews->get_subbyid($sub);
		$row=$mnews->fetchOne();
		if($row)
		{
			$row['url']=seo_subcategory($row['sub_id'],$row['sub_alias']);
			$cache->save("sub".$sub,$row,10000);
			return $row;
		}
		else
		{
			return false;
		}
		
	}
	return $sub_info;
	
}

function category($where){
	$db=new db;
	$db->connect();
	$db->query("select * from category  where  ".$where." order by  thutu asc");
	$data=$db->fetchAll();
	return $data;
}
        
        
  function createTagCloud()  
{     
    $db=new db;
	$db->connect();
	$db->query("select * from tag");
    $total_tuts =$db->num_rows();  
	$tags=$db->fetchAll();
    
      

      
    //ugh, XHTML in PHP?  Slap my hands - this isn't best practice, but I was obviously feeling lazy  
    $html = '<div class="tagcloud">';  
    
    //iterate through each item in the $output array (created above)  
    foreach($tags as $tag)  
    {  
        //get the number-of-tag-occurances as a percentage of the overall number  
        $ratio = (100 / $total_tuts) * $tag['num'];  
          
        //round the number to the nearest 10  
        $ratio =  round($ratio,-1);  
          
        /*append that classname onto the list-item, so if the result was 20%, it comes out as cloud-20*/  
        $html.= '<a  class="tag-link" href=http://localhost/xgrid/tag/'.khongdau($tag['tag']).'/ title="'.$tag['tag'].'">'.$tag['tag'].'</a>';           
    }  
      
    //close the UL  
    $html.= '</div>';  
    $db->close();
    return $html;   
}   

function tagscloud(){
    $cache=new cache;
	$tagcloud=$cache->load("tagscloud");
	if(!$tagcloud){
		$tagcloud=createTagCloud();
		$cache->save("tagscloud",$tagcloud,1000);
	}
	return $tagcloud;
}


function news_home_hilight(){
		$cache=new cache;
		$data=$cache->load("home_hilight","news");
		if(!$data){
					//CONNECT DB
					
					$db=new db;
					$db->connect();
					// GET HOME HILIGHT NEWS
					$db->query("select  * from game_category where main=0");
					if($db->num_rows()>0){
						
						$hl=$db->fetchOne();
					}
					
					// GET HOME STICKY NEWS
					$db->query("select * from game_category where main !=0");
					if($db->num_rows()>0){
						   $sk=$db->fetchAll();
					}else{
						   $sk=false;
					}
					
					//ASSIGN DATA TO ARRAY
					$data['hl']=$hl;
					$data['sk']=$sk;
					// SAVE TO CACHE NEWS DIRECTORY
					$cache->save("home_hilight",$data,600,"news");
		}
		
		// DISPLAY DATA TO HTML
		$hl=$data['hl'];
		?>
        

<!-- START HILIGHT GAME -->
 <div style="background-color:#000">
    	<div class="row wrap_1180" style="padding-bottom:10px;" >
    
    	<div class="col-md-4">
    
     <Br/>
        	<h3 style="color:#FFF"><?php echo $hl['tieude']; ?></h3>
            <p style="font-size:15px; color:#CCC; line-height:20px; word-spacing:3px">
            <?php echo $hl['sdes']; ?>
            </p>
    		
     		


<div class="btn-group btn-group-lg">
  <a href="<?php echo $hl['url'] ?>" class="btn btn-danger">Xem Chi Tiết <span class="glyphicon glyphicon-folder-open"></span></a>
  <a href="<?php if(!isset($_SESSION['user'])){ echo LOGIN;}else{ echo $_SESSION['user']['p2w']; } ?>" class="btn btn-success">Chơi Ngay <span class="glyphicon glyphicon-play" ></span> </a>
  
</div>
   
    </div>
    
   <div class="col-md-8">
   
      <img src="<?php echo IMAGE.$hl['lpicture'] ?>" class="img-rounded"  >
  
    
        </div>
       
       
 </div> <!-- End of Row -->
  </div>
<!-- END OF HILIGHT GAME -->

<!-- LIST GAME -->
<!-- START BLOG LEFT -->
<div class="list_game" >
<div style="max-width:1180px; margin:auto ; padding-top:20px">
	
    <?php
	$j=1;
	foreach($data['sk'] as $row){
		if($j%4==1){
			echo  "<div class='row' style='padding-bottom:10px;'>";
		}
		?>
	  
        <div class="col-md-3">
        <a href="<?php echo $row['url'] ?>" > <img src="<?php echo IMAGE.$row['picture'] ?>" class="img-rounded pull-left" style="margin-right:10px"/>  </a>
        <h4>  <a href="<?php echo $row['url'] ?>" ><?php echo $row['tieude'] ?></a></h4>
         <img src="http://mxhgame.com/public/templates/public/tml_update/images/star.png" />
      
        <p><small><?php echo $row['sdes'] ?></small></p>
      
        </div>
	<?php	
			if($j%4==0){
			echo  "</div>";
		}
		$j++;
	}
	?>

</div>
</div>

<!-- END OF LIST GAME -->



        <?php

}

function news_home_hilight_game(){
	$cache=new cache;
		$data=$cache->load("home_hilight","news");
		if(!$data){
					//CONNECT DB
					
					$db=new db;
					$db->connect();
					// GET HOME HILIGHT NEWS
					$db->query("select  * from game_category where main=0");
					if($db->num_rows()>0){
						
						$hl=$db->fetchOne();
					}
					
					// GET HOME STICKY NEWS
					$db->query("select * from game_category where main !=0");
					if($db->num_rows()>0){
						   $sk=$db->fetchAll();
					}else{
						   $sk=false;
					}
					
					//ASSIGN DATA TO ARRAY
					$data['hl']=$hl;
					$data['sk']=$sk;
					// SAVE TO CACHE NEWS DIRECTORY
					$cache->save("home_hilight",$data,600,"news");
		}
		
		// DISPLAY DATA TO HTML
		$hl=$data['hl'];
		?>
	

<!-- START HILIGHT GAME -->
 <div style="background-color:#000">
    	<div class="row wrap_1180" style="padding-bottom:10px;" >
    
    	<div class="col-md-4">
    
     <Br/>
        	<h3 style="color:#FFF"><?php echo $hl['tieude']; ?></h3>
            <p style="font-size:15px; color:#CCC; line-height:20px; word-spacing:3px">
            <?php echo $hl['sdes']; ?>
            </p>
    		
     		


<div class="btn-group btn-group-lg">
  <a href="<?php echo $hl['url'] ?>" class="btn btn-danger">Xem Chi Tiết <span class="glyphicon glyphicon-folder-open"></span></a>
  <a href="<?php if(!isset($_SESSION['user'])){ echo LOGIN;}else{ echo $_SESSION['user']['p2w']; } ?>" class="btn btn-success">Chơi Ngay <span class="glyphicon glyphicon-play" ></span> </a>
  
</div>
   
    </div>
    
   <div class="col-md-8">
   
      <img src="<?php echo IMAGE.$hl['lpicture'] ?>" class="img-rounded"  >
  
    
        </div>
       
       
 </div> <!-- End of Row -->
  </div>
<!-- END OF HILIGHT GAME -->
<?php
}


function news_list($data,$nav,$type="",$pos=""){
		 if($data)
		  foreach($data as $row){
				?>
                    <div class="row" style="margin-bottom:5px; padding-bottom:10px; border-bottom:1px dashed #CCC"> <!-- START BLOG  ITEM-->
                    		<div class="col-md-3"><img src="<?php  echo $row['hinhanh']?>"  style="max-width:175px" /></div>
                            <div class="col-md-9">
                            	  <h3 style="padding-bottom:5px; margin-top:3px; margin-bottom:5px;"><a href="<?php echo  seo_baiviet($row['id'],$row['seo_tieude'])?>"><?php  echo $row['tieude']?></a></h3>
                                   	<p>    	 <?php  echo $row['gioithieu']?>                         </p>
                            </div>	
                              
                                	
                                
                                 
									
					 </div> <!-- END OF ROW  ITEM-->
					 <?php 
						  }
			 echo $nav;?>
		  
<?php                    
}
function news_list_grid($data,$nav,$type="",$pos=""){
			echo "<div id='device'>";
		 if($data)
		  foreach($data as $row){
				?>
                    <div class="item"> <!-- START BLOG  ITEM-->
                   				 <div class="item_image">
											<?php if($row['hinhanh']!=""){ ?>
    								         <a href="<?php echo  seo_baiviet($row['id'],$row['seo_tieude'])?>">   <img src="<?php  echo $row['hinhanh']?>" class="img-rounded" /></a>
                          				 	 <?php }?>
                                </div>
                                
                                  <div class="item_content">
                                   <a href="<?php echo  seo_baiviet($row['id'],$row['seo_tieude'])?>"><?php  echo $row['tieude']?></a>
                                   
                                 
                                 
                                    </div>   
                                        <div class="item_info">
                                              	<p class="item_des">
                                 		 <?php  echo $row['gioithieu']?></span>
                                    </p>	
                                        </div>
									
                           
                          		
                    </div> <!-- End of item class -->
                      
                    <?php 
					  
				  }
				  echo "</div>";
			 echo $nav;?>
                  <script src="grid/jquery.grid-a-licious.js"></script>
                  <script>
                   	$('#device').gridalicious({
				gutter: 20,
				width: 190,
				animate: true,
			  	animationOptions: {
				    queue: true,
				    speed: 100,
				    duration: 300,
				    effect: 'fadeInOnAppear',
			  	}	
			});
                  </script>
		  
<?php                    
}
function news_home_game(){
		$cache=new cache;
		$data=$cache->load("news_home_game","news");
		if(!$data){
			$db=new db;
			$db->connect();
			$db->query("select * from news_category where game_home=1 order by cat_id desc limit 0,4");
			if($db->num_rows()>0){
			$data=$db->fetchAll();
			}else{
			
				$data=false;
			}
			$cache->save("news_home_game",$data,600,"news");
		}
		
		if($data){?>
        
          <div class="row-fluid news_home_game">
    
    							<?php foreach($data as $row){?>
									
							        
                                <div class="span3 db_game_item2">
                                        <div class="db_game_title2">
                                                <a href="<?php echo seo_category($row['cat_id'],$row['cat_alias']) ?>"><img src="<?php echo HOME."public/uploads/".$row['simage'] ?>" width="140" height="140"  ></a>
                
                                                <h5><a href="<?php echo seo_category($row['cat_id'],$row['cat_alias']);?>" title="Game avatar"><?php echo $row['cat_title'];?></a>  </h5>
                                  
        
                                        </div>

<div class="fb-like" data-href="<?php echo seo_category($row['cat_id'],$row['cat_alias']);?>" data-send="false" data-layout="button_count" data-width="50" data-show-faces="false" data-font="lucida grande"></div>
                                   	 <div class="db_game_descr">
											 <?php echo $row['game_theloai'];?>
                                     
									</div>

                                </div> <!-- END OF SPAN3 -->
                                <?php } //end of foreach
								?>
                                
</div> <!-- Row fluid -->
		

 <?php } // END OF IF($data)
}

function news_game_info($cat_info){

	 if($cat_info['type']!='news'){ ?>    
   		<div class="row row-fluid wrapgame" >
             <div class='span6' >                  
                   		<img   alt="<?php echo $cat_info['cat_title'] ?>" src="<?php  echo IMAGE.$cat_info['simage']?>"   align="left" class="img-rounded" style="margin-right:10px"  >
                                <div><a href="<?php  echo $cat_info['url']?>"><b><?php echo $cat_info['cat_title'] ?></b></a></div>
                                 <div> <?php echo $cat_info['ldes'] ?></div>
                                
              <div class="fb-like" data-href="<?php  echo $cat_info['url']?>" data-send="true" data-width="100" data-show-faces="true" data-font="lucida grande"></div>                 
			</div>
    		<div class="span6" >
                    <ul>
                    <li><span class="game_main_info">Thể Loại :</span><span class="game_main_info_detail"><?php echo $cat_info['game_theloai'] ?></span></li>
                    <li><span class="game_main_info">Nhà Sản Xuất :</span><span class="game_main_info_detail"><?php echo $cat_info['game_nph'] ?></span></li>
                    <li><span class="game_main_info">Version :</span><span class="game_main_info_detail"><?php echo $cat_info['game_version'] ?></span></li>
                    <li><span class="game_main_info">Hỗ Trợ :</span><span class="game_main_info_detail"><?php echo $cat_info['game_hotro'] ?></span></li>
                    <li>
                    <table class="table ">
				<thead>
                <tr>
                  <th colspan="3" align="center">Tải Game</th>
			   </tr>
              </thead>
              <tbody>
                <tr>
                  <td >Soạn Tin Nhắn</td>
                
                  <td> <?php echo $cat_info['sms'] ?></td>
                </tr>
                <tr>
                <td >Trực Tiếp</td>
                  <td><a href=" <?php echo $cat_info['direct'] ?>" class="btn btn-large btn-success"><i class="icon-download" ></i> Tải Game</a></td>
                </tr>
              
              </tbody>
            </table></li>
                   
                   
                   

                    </ul>
    </div>              



	</div> <!-- End of row fluid --> 
      <?php
		}

}

function news_game_info_wap($cat_info){

	 if($cat_info['type']!='news'){ ?>    
   		<div class="row row-fluid wrapgame" >
             <div class='span12' >                  
                   		<img   alt="<?php echo $cat_info['cat_title'] ?>" src="<?php  echo IMAGE.$cat_info['simage']?>"   align="left" class="img-rounded" style="margin-right:10px"  >
                                <div><a href="<?php  echo $cat_info['url']?>"><b><?php echo $cat_info['cat_title'] ?></b></a></div>
                                 <div> <?php echo $cat_info['ldes'] ?></div>
                                
              <div class="fb-like" data-href="<?php  echo $cat_info['url']?>" data-send="true" data-width="100" data-show-faces="true" data-font="lucida grande"></div>                 
			</div>
    		<div class="span12" >
                    <ul>
                    <li><span class="game_main_info">Thể Loại :</span><span class="game_main_info_detail"><?php echo $cat_info['game_theloai'] ?></span></li>
                    <li><span class="game_main_info">Nhà Sản Xuất :</span><span class="game_main_info_detail"><?php echo $cat_info['game_nph'] ?></span></li>
                    <li><span class="game_main_info">Version :</span><span class="game_main_info_detail"><?php echo $cat_info['game_version'] ?></span></li>
                    <li><span class="game_main_info">Hỗ Trợ :</span><span class="game_main_info_detail"><?php echo $cat_info['game_hotro'] ?></span></li>
                
                   
                   
                   

                    </ul>
    </div>              



	</div> <!-- End of row fluid --> 
      <?php
		}

}
function news_game_wap($cat_info){?>

                   
                   
                    <?php
					$db=new db;
					$db->connect();
					
					if($cat_info['android']==1){
						$db->query("select * from dl where os='android' and game=".$cat_info['cat_id']);
						if($db->num_rows()>0){
						$data[android]=$db->fetchAll();
						}
				}
					
					
						if($cat_info['java']==1){
						$db->query("select * from dl where os='java' and game=".$cat_info['cat_id']);
						if($db->num_rows()>0){
						$data['java']=$db->fetchAll();
						}
					}
					
						
						if($cat_info['ios']==1){
						$db->query("select * from dl where os='ios' and game=".$cat_info['cat_id']);
						if($db->num_rows()>0){
						$data[ios]=$db->fetchAll();
						}
					}
					$db->close();
				
					 ?>
                      <table class="table table-bordered wap">
                      <?php if($data['android']){ ?>
              <thead>
                <tr>
                  <th colspan="2"><?php echo Android ?></th>
                
                 
                </tr>
              </thead>
              <tbody>
                <?php foreach($data['android'] as $row){ ?>
                <tr>
                  <td ><?php echo $row['name']; ?></td>
                  <td><a href="<?php echo $row['link']; ?>"><?php echo $row['file']; ?></a></td>
               
                </tr>
                <?php
				}?>
               
             
              </tbody>
              <?php
					  }
			  ?>
              
              
                              <?php if($data['java']){ ?>
              <thead>
                <tr>
                  <th colspan="2"><?php echo "Java"; ?></th>
                
                 
                </tr>
              </thead>
              <tbody>
                <?php foreach($data['java'] as $row){ ?>
                <tr>
                  <td ><?php echo $row['name']; ?></td>
                  <td><a href="<?php echo $row['link']; ?>"><?php echo $row['file']; ?></a></td>
               
                </tr>
                <?php
				}?>
               
             
              </tbody>
              <?php
					  }
			  ?>
                                    <?php if($data['ios']){ ?>
              <thead>
                <tr>
                  <th colspan="2"><?php echo "IOS"; ?></th>
                
                 
                </tr>
              </thead>
              <tbody>
                <?php foreach($data['ios'] as $row){ ?>
                <tr>
                  <td ><?php echo $row['name']; ?></td>
                  <td><a href="<?php echo $row['link']; ?>"><?php echo $row['file']; ?></a></td>
               
                </tr>
                <?php
				}?>
               
             
              </tbody>
              <?php
					  }
			  ?>
              
           
            </table> 
            <?php
}
?>